<template>
  <div style="width: 100%">
    <div id="deawer">
      <v-tableTitle
        :btnConfig="btnConfig"
        @cancel="cancel"
        @save="save"
      ></v-tableTitle>
      <div class="detailBox">
        <v-detail ref="detail"></v-detail>
      </div>
      <div class="detailListBox">
        <v-detailList ref="detailList"></v-detailList>
      </div>
    </div>
  </div>

</template>

<script>
import baseMixin from "@/mixins/BaseMixin";
import skipMixin from "@/mixins/SkipMixin";
import tableTitle from "commons/TableTitle";
import detail from "../details/Detail"
import detailList from "../detailsList/DetailList"

export default {
  name: "",
  mixins: [baseMixin, skipMixin],
  data () {
    return {
      newFlag: false,
    };
  },
  computed: {
    btnConfig () {
      return [
        { label: this.$t('common.save'), certigier: this.saveStatus||!this.btnPermNameList.includes('WM03070103'), invalid: "save" },
        { label: this.$t('common.cancel'),certigier: !this.btnPermNameList.includes('WM03070104'), invalid: "cancel" },
      ]
    }
  },
  created () {
  },
  methods: {
    //保存按钮
    async save () {
      this.$refs.detail.$children[0].$refs.input.$refs["from"].validate(async (valid) => {
        if (valid) {
          let detailsData = this.$refs.detail.detailData;
          let datailsListData = this.$refs.detailList.detailData;
          if (this.newFlag) {
            this.$refs.detailList.$children[2].$refs.input.$refs["from"].validate(async (flag) => {
              if (flag) {
                let data = {
                  ...detailsData,
                  putawaystrategydetailList: [
                    {
                      ...datailsListData,
                    }
                  ]
                };
                try {
                  let res = await this.$api.post('/addputawaystrategy', data);
                  
                    this.$message.success(res.returnMgr);
                    this.cancel()
                } catch (e) {
                  this.$message.error(e);
                }
              }
            })
          } else {
            if (this.$refs.detailList.detailNav !== "1") {
              this.$refs.detailList.$children[2].$refs.input.$refs["from"].validate(async (flag) => {
                if (flag) {
                  if (this.$refs.detailList.detailsFlag) {
                    try {
                      let res = await this.$api.post('/queryputawaydetailmaxlinenumber', this.$refs.detailList.detailData);
                      
                        let data = {
                          ...this.$refs.detailList.detailData,
                          putawaystrategylinenumber: res.data - 0 + 1
                        };
                        try {
                          let response = await this.$api.post('/addputawaystrategydetail', data);
                            this.$message.success(response.returnMgr);
                            this.$refs.detailList.detailsFlag = false;
                            this.$refs.detailList.detailNav = '1';
                        } catch (e) {
                          this.$message.error(e);
                        }
                      
                    } catch (e) {
                      this.$message.error(e);
                    }
                  } else {
                    try {
                      let response = await this.$api.post('/updateputawaystrategydetail', this.$refs.detailList.detailData);
                      
                        this.$message.success(response.returnMgr);
                        this.$refs.detailList.detailNav = '1';
                    } catch (e) {
                      this.$message.error(e);
                    }
                  }
                }
              })

            } else {
              try {

                let res = await this.$api.post('/updateputawaystrategy', detailsData);
                
                  this.$message.success(res.returnMgr);
                  this.cancel()
              } catch (e) {
                this.$message.error(e);
              }
            }

          }
        }
      })
    },
    //弹框取消
    cancel () {
      this.$router.push({ name: "putaway" });
    },
  },
  mounted () {
    this.newFlag = this.$store.state.newFlag;
    if (Object.keys(this.$store.state.skip).length !== 0) {
      let data = this.$store.state.skip;
      this.newFlag = data.newFlag;
    }
  },
  watch: {},
  components: {
    "v-tableTitle": tableTitle,
    "v-detail": detail,
    "v-detailList": detailList,
  }
}
</script>

<style scoped>
</style>
